package com.jushu.storbox.adapter;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.entities.AlbumEntity;
import com.jushu.storbox.util.image.AbsImageLoader;
import com.jushu.storbox.util.image.AbsImageLoader.LoadType;

public class AlbumCatchSettingAdapter extends AbsTemplateAdapter<AlbumEntity> {

	protected final static int LOAD_IMAGE_SUCCESS = 1;
	protected final static int LOAD_IMAGE_FAILURE = 0;

	Context context;
	LayoutInflater inflater;

	public AlbumCatchSettingAdapter(Context context, List<AlbumEntity> translateList) {
		super(translateList);
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder holder = null;
		if (arg1 == null) {
			holder = new ViewHolder();
			arg1 = holder.getLayoutView(arg1, arg2);
			arg1.setTag(holder);
		} else {
			holder = (ViewHolder) arg1.getTag();
		}

		holder.initListener();
		holder.BinddingViewByHolderDate(getItem(arg0));

		return arg1;
	}

	class ViewHolder {

		View contentView = null;
		ImageView group_image = null;
		CheckBox child_checkbox = null;
		TextView group_count = null;
		TextView group_title = null;

		Bitmap contentBitmap = null;

		AlbumEntity albumEntity;
		AbsImageLoader loader = null;

		Handler selfHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case LOAD_IMAGE_SUCCESS:
					Bitmap oldBitmap = contentBitmap;
					contentBitmap = (Bitmap) msg.obj;
					if (contentBitmap != null && !contentBitmap.isRecycled()) {
						group_image.setImageBitmap(contentBitmap);
					} else {
						group_image.setImageResource(R.mipmap.empty_pic);
					}
					// recycle oldBitmap
					 if (oldBitmap != null && !oldBitmap.isRecycled()) {
					 oldBitmap.recycle();
					 oldBitmap = null;
					 }
					break;

				case LOAD_IMAGE_FAILURE:
					group_image.setImageResource(R.mipmap.empty_pic);
					break;

				default:
					super.handleMessage(msg);
				}
			}
		};

		public void initListener() {
			contentView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (albumEntity.isSelected()) {
						albumEntity.setSelected(false);
						child_checkbox.setChecked(false);
					} else {
						albumEntity.setSelected(true);
						child_checkbox.setChecked(true);
					}
				}
			});

			child_checkbox.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (albumEntity.isSelected()) {
						albumEntity.setSelected(false);
					} else {
						albumEntity.setSelected(true);
					}
				}
			});
		}

		public View getLayoutView(View storaView, ViewGroup parent) {
			contentView = inflater.inflate(R.layout.album_grid_child_item, parent, false);
			group_image = (ImageView) contentView.findViewById(R.id.group_image);
			child_checkbox = (CheckBox) contentView.findViewById(R.id.child_checkbox);
			group_count = (TextView) contentView.findViewById(R.id.group_count);
			group_title = (TextView) contentView.findViewById(R.id.group_title);
			return contentView;
		}

		public void BinddingViewByHolderDate(AlbumEntity albumEntity) {
			this.albumEntity = albumEntity;
			loader = new AbsImageLoader() {
				protected void onLoadSuccess(Bitmap mBitmap) {
					selfHandler.obtainMessage(LOAD_IMAGE_SUCCESS, mBitmap).sendToTarget();
				}

				protected void onLoadFailure(String imagePath, LoadType failureType) {
					selfHandler.obtainMessage(LOAD_IMAGE_FAILURE).sendToTarget();
				}
			};

			group_image.setImageResource(R.mipmap.empty_pic);
			if (albumEntity.isSelected()) {
				child_checkbox.setChecked(true);
			} else {
				child_checkbox.setChecked(false);
			}
			group_title.setText(albumEntity.getParent().getName());
			List<File> childs = albumEntity.getChildList();
			int size = childs.size();
			group_count.setText("" + size);
			if (size > 0) {
				loader.loadImage(childs.get(0).getAbsolutePath());
			}
		}
	}
}
